package com.example.demo.utils;
import com.example.demo.APIS.DApi;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;


/**
 * Created by cherryzou on 2018/1/21.
 * @Desc the base class for upload the picture
 */
public class FileUtil {

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);

        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        saveFile(fileName,file);
        out.write(file);

        out.flush();
        out.close();
    }



    public static int saveFile(String filename,byte[] buffer){

        // 1. 保存到 oss
        // 2. 使用 byte 保存文件
        // 3. 文件名称使用 md 5
        OSS ossClient = null;

        try {
            ossClient = new OSSClientBuilder().build(DApi.endpoint, DApi.accessKeyId, DApi.accessKeySecret);


            PutObjectRequest putObjectRequest = new PutObjectRequest(DApi.bucketName, filename,new ByteArrayInputStream(buffer));

            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
            if(putObjectResult.getResponse() != null){
                putObjectResult.getResponse().getContent().read(buffer);
                putObjectResult.getResponse().getContent().close();
            }


        } catch (IOException e) {
            e.printStackTrace();

        }finally {
            if(ossClient != null){
                // 关闭client
                ossClient.shutdown();
            }
        }

        return  0 ;
    }

    /**

     * @desc 获取图片 http url 地址
     *
     */
    public static String getURL(String filename){

        // 1. 通过 filename 生成 md5
        // 2. 使用 oss 获取 ossclient
        // 3. 通过 ossclient 加上超时
        OSS ossClient = null ;
        try {


            ossClient =  new OSSClientBuilder().build(DApi.endpoint, DApi.accessKeyId, DApi.accessKeySecret);
            // 设置URL过期时间为 5 天
            Date expiration = new Date(new Date().getTime() + (((3600 * 1000) * 24) * 5) );
            // 生成URL
            URL url = ossClient.generatePresignedUrl(DApi.bucketName, filename, expiration);
           System.out.println("oss 图片URL:"+ url.toString());
            return url.toString();
        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        }finally {
            if(ossClient != null){
                // 关闭client
                ossClient.shutdown();
            }
        }

    }

    /**

     * @desc 用途：判断是否存在图片
     *
     */
    public static Boolean isFileName(String filename){

        boolean found = false ;
        // 1. 通过 filename 进行 md5
        // 2. 通过 ossclient 获取 图片 是否存在

        OSS ossClient = null ;
        try {

            ossClient = new OSSClientBuilder().build(DApi.endpoint, DApi.accessKeyId, DApi.accessKeySecret);
            // Object是否存在
            found = ossClient.doesObjectExist(DApi.bucketName, filename);
            System.out.println("oss 是否保存图片:"+ found);

            return found;
        } catch (OSSException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }finally {
            if(ossClient != null){
                // 关闭client
                ossClient.shutdown();
            }
        }
        return false ;

    }

}
